import 'package:enjoy_plus_hm/pages/Home/index.dart';
import 'package:enjoy_plus_hm/pages/Mine/index.dart';
import 'package:enjoy_plus_hm/utils/eventbus.dart';
import 'package:flutter/material.dart';

import '../../utils/token.dart';

class TabBarPage extends StatefulWidget {
  @override
  State<TabBarPage> createState() => _TabBarPageState();
}

class _TabBarPageState extends State<TabBarPage> {
  @override
  void initState() {
    super.initState();
    // 先获取token
    Future.delayed(Duration.zero, () async {
      await TokenManager().init();
    });
    eventBus.on<logoutEvent>().listen((event) {
      Navigator.pushNamed(context, '/login');
    });
  }

  int currentIndex = 0;
  // 底部TabBar数据
  List tabBarList = [
    {
      "title": "首页",
      "default": "assets/tabs/home_default.png",
      "active": "assets/tabs/home_active.png",
    },
    {
      "title": "我的",
      "default": "assets/tabs/my_default.png",
      "active": "assets/tabs/my_active.png",
    },
  ];
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: SafeArea(
        child: IndexedStack(
          index: currentIndex,
          children: [HomePage(), MinePage(currentIndex: currentIndex)],
        ),
      ),
      bottomNavigationBar: BottomNavigationBar(
        type: BottomNavigationBarType.fixed,
        selectedItemColor: const Color.fromARGB(255, 85, 145, 175),
        unselectedItemColor: Colors.black,
        currentIndex: currentIndex,
        items: tabsList(),
        onTap: (e) {
          setState(() {
            currentIndex = e;
          });
        },
      ),
    );
  }

  List<BottomNavigationBarItem> tabsList() {
    List<BottomNavigationBarItem> items = [];
    for (var item in tabBarList) {
      items.add(
        BottomNavigationBarItem(
          icon: Image.asset(item['default'], width: 32, height: 32),
          activeIcon: Image.asset(item['active'], width: 32, height: 32),
          label: item['title'],
        ),
      );
    }
    return items;
  }
}
